package com.distributeredis.redis_springboot.spring_fast_json.d02_serilizerfeature;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.distributeredis.redis_springboot.spring_fast_json.d01_fastjsontest.UserPeople;
import org.junit.Test;

/**
 * SerializerFeature 枚举：进行序列化时，可以自己定义特殊需求
 * JSON静态方法， toJSONString()
 * 方法的参数：第一个是要序列化的对象
 * 方法的参数：第二个参数SerializerFeature枚举类型的可变参数
 * SerializerFeature枚举常量，做序列化的个性需求
 */
public class TestFastJson2 {
    @Test
    public void test1() {
        UserPeople userPeople = new UserPeople();
        userPeople.setUserName("zs");
        userPeople.setUserAge(22);
        userPeople.setUserBirthday("2222-11-11");
//        userPeople.setUserAddress("河南");
        String toJSONString = JSON.toJSONString(userPeople);
        System.out.println(toJSONString); // {"userAge":22,"userBirthday":"2222-11-11","userName":"zs"}
        String toJSONString1 = JSON.toJSONString(userPeople, SerializerFeature.WriteMapNullValue);
        System.out.println(toJSONString1);
        //{"userAge":22,"userBirthday":"2222-11-11","userName":"zs"}
        //{"userAddress":null,"userAge":22,"userBirthday":"2222-11-11","userName":"zs"}

        /**
         * 枚举常量 WriteMapNullValue 序列化为null的字段
         * 枚举常量 WriteNullStringAsEmpty 字段为null,序列化为""
         * 枚举常量 WriteNullNumberAsZero 字段为null,序列化为0  针对int类型
         * - 枚举常量 WriteNullBooleanAsFalse 字段值为null 输出false
         * - 枚举常量 WriteDateUseDateFormat  格式化日期格式
         * - 枚举常量 PrettyFormat格式化输出
         *  String jsonString = JSON.toJSONString(person,SerializerFeature.WriteNullBooleanAsFalse,
         *   SerializerFeature.WriteDateUseDateFormat,SerializerFeature.PrettyFormat);
         */
    }
}
